<template>
  <div>
    <el-form label-width="150px">
      <el-form-item :label="$t('apiKey')">
        <el-input v-model="userSecret.apiKey" :disabled="true"/>
      </el-form-item>
      <el-form-item :label="$t('apiSecret')">
        <el-input v-model="userSecret.apiSecret" :disabled="true"/>
      </el-form-item>
      <el-form-item :label="$t('token')">
        <el-button
          type="primary"
          icon="el-icon-s-ticket"
          @click="insertOrUpdateAndGetUserSecretToken()"
        >{{ $t('token') }}
        </el-button>
      </el-form-item>
    </el-form>

    <el-dialog
      :visible.sync="dialogTokenVisible"
    >
      <el-form
        label-width="120px"
      >
        <el-form-item :label="$t('token')">
          <el-input v-model="secretToken.token" disabled/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogTokenVisible = false">{{ $t('cancel') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getUserId } from '@/util/storage'
import { insertOrUpdateAndGetUserSecretToken } from '@/api/auth/authUserSecretTokenApi'
import { getUserSecret } from '@/api/auth/authUserApi'

export default {
  name: 'Secret',
  data() {
    return {
      userSecret: {
        apiKey: null,
        apiSecret: null
      },
      secretToken: {},
      userId: getUserId(),
      dialogTokenVisible: false
    }
  },
  created() {
    this.showDevInfo()
  },
  methods: {
    insertOrUpdateAndGetUserSecretToken() {
      insertOrUpdateAndGetUserSecretToken({
        userId: this.userId
      }).then(res => {
        if (res.data) {
          this.secretToken = res.data
          this.dialogTokenVisible = true
        }
      })
    },
    showDevInfo() {
      getUserSecret().then(res => {
        const { code, data } = res
        if (code === '200') {
          this.userSecret = data
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
